package com.zkingsoft.services.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zkingsoft.dao.FunctionDao;
import com.zkingsoft.model.Company;
import com.zkingsoft.model.Function;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.StringUtils;
import com.zkingsoft.services.FunctionService;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2016-04-22 08:43
 */
@Service("functionService")
public class FunctionServiceImpl implements FunctionService {

	@Autowired
	private FunctionDao functionDao;

	@Override
	public int add(Function function) {

		return functionDao.insert(function);

	}

	@Override
	public int modify(Function function) {

		return functionDao.update(function);

	}

	@Override
	public int remove(List<Long> list) {
		int i = 0;
		for (Long id : list) {
			i += removeFunctions(id);
		}
		return i;
	}

	@Override
	public int removeById(Long id) {

		return removeFunctions(id);
	}

	private int removeFunctions(Long id) {
		Function fnquery = new Function();
		fnquery.setParentId(id);
		// 获取子节点
		List<Function> list = functionDao.selectByModel(fnquery);
		if (list.size() > 0) {
			for (Function function : list) {
				// 删除子节点
				removeFunctions(function.getId());
			}
		}
		return functionDao.deleteById(id);
	}

	@Override
	public List<Function> findInPage(Function function, PaginationVO pageVo) {

		return functionDao.selectInPage(function, pageVo);

	}

	@Override
	public List<Function> findByModel(Function function) {

		return functionDao.selectByModel(function);

	}

	@Override
	public int findTotal(Function function) {

		return functionDao.selectTotalRecord(function);

	}

	@Override
	public Function findById(Long id) {

		return functionDao.selectById(id);

	}

	@Override
	public List<Function> findByIds(List<Long> ids) {
		// TODO Auto-generated method stub
		return functionDao.selectByIds(ids);
	}

	@Override
	public List<Function> findRoleFunctionByGrade(Long roleId, int grade) {
		// TODO Auto-generated method stub
		return functionDao.selectRoleFunctionByGrade(roleId, grade);
	}

	@Override
	public List<Function> selectRoleFunctionByParent(Long roleId, long parentId) {
		// TODO Auto-generated method stub
		return functionDao.selectRoleFunctionByParent(roleId, parentId);
	}

	/**
	 * 筛选出本公司具有的功能
	 * 
	 * @param functions
	 * @param request
	 * @return
	 */
	public List<Function> filterFunction(List<Function> functions, Company company) {
		List<Long> ids = StringUtils.strToCollToLong(company.getFunctions(), ",");
		List<Function> filterdfunctions = new ArrayList<>();
		for (Function oneFunction : functions) {
			if (ids.contains(oneFunction.getId())) {
				filterdfunctions.add(oneFunction);
			}
		}
		return filterdfunctions;
	}

}